package com.fairisanddog.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.fairisanddog.entity.Books;
import com.fairisanddog.entity.Users;
import com.fairisanddog.service.BooksService;
import com.fairisanddog.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoaderListener;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UsersController {

    @Autowired()
    @Qualifier("UsersServiceImpl")
    private UsersService usersService;

    @Autowired()
    @Qualifier("BooksServiceImpl")
    private BooksService booksService;


    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "test";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody Users users, HttpServletRequest request, Model model){

        HttpSession session = request.getSession();
        List<Books> books = booksService.queryBookByType("小说");
        String a = JSON.toJSONString(books);
        session.setAttribute("myLikes",a);
       Users users1 = usersService.queryUsersByNameAndPwd(users.getuName(),users.getuPwd());
       if (users1!=null){
            String s = JSON.toJSONString(users1);
            return s;
       }else{
            return "no";
       }

    }


}
